home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdHLineProps.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  6.3 KB  |  210 lines

  1. /* 
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *  
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *  
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  * 
  20.  * Contributor(s): 
  21.  */
  22.  
  23. var tagName = "hr";
  24. var hLineElement;
  25. var width;
  26. var height;
  27. var align;
  28. var shading;
  29. const gMaxHRSize = 1000; // This is hard-coded in nsHTMLHRElement::StringToAttribute()
  30.  
  31. // dialog initialization code
  32. function Startup()
  33. {
  34.   if (!InitEditorShell())
  35.     return;
  36.  
  37.   // Get the selected horizontal line
  38.   hLineElement = editorShell.GetSelectedElement(tagName);
  39.  
  40.   if (!hLineElement) {
  41.     // We should never be here if not editing an existing HLine
  42.     window.close();
  43.     return;
  44.   }
  45.   gDialog.heightInput = document.getElementById("height");
  46.   gDialog.widthInput = document.getElementById("width");
  47.   gDialog.leftAlign = document.getElementById("leftAlign");
  48.   gDialog.centerAlign = document.getElementById("centerAlign");
  49.   gDialog.rightAlign = document.getElementById("rightAlign");
  50.   gDialog.alignGroup = gDialog.rightAlign.radioGroup;
  51.   gDialog.shading = document.getElementById("3dShading");
  52.   gDialog.pixelOrPercentMenulist = document.getElementById("pixelOrPercentMenulist");
  53.  
  54.   // Make a copy to use for AdvancedEdit and onSaveDefault
  55.   globalElement = hLineElement.cloneNode(false);
  56.  
  57.   // Initialize control values based on existing attributes
  58.   InitDialog()
  59.  
  60.   // SET FOCUS TO FIRST CONTROL
  61.   SetTextboxFocus(gDialog.widthInput);
  62.  
  63.   // Resize window
  64.   window.sizeToContent();
  65.  
  66.   SetWindowLocation();
  67. }
  68.  
  69. // Set dialog widgets with attribute data
  70. // We get them from globalElement copy so this can be used
  71. //   by AdvancedEdit(), which is shared by all property dialogs
  72. function InitDialog()
  73. {
  74.   // Just to be confusing, "size" is used instead of height because it does
  75.   // not accept % values, only pixels
  76.   var height = GetHTMLOrCSSStyleValue(globalElement, "size", "height")
  77.   height = StripPxUnit(height);
  78.   if(!height) {
  79.     height = 2; //Default value
  80.   }
  81.  
  82.   // We will use "height" here and in UI
  83.   gDialog.heightInput.value = height;
  84.  
  85.   // Get the width attribute of the element, stripping out "%"
  86.   // This sets contents of menulist (adds pixel and percent menuitems elements)
  87.   gDialog.widthInput.value = InitPixelOrPercentMenulist(globalElement, hLineElement, "width","pixelOrPercentMenulist");
  88.  
  89.   var marginLeft  = GetHTMLOrCSSStyleValue(globalElement, "align", "margin-left").toLowerCase();
  90.   var marginRight = GetHTMLOrCSSStyleValue(globalElement, "align", "margin-right").toLowerCase();
  91.   align = marginLeft + " " + marginRight;
  92.   gDialog.leftAlign.checked   = (align == "left left"     || align == "0px auto");
  93.   gDialog.centerAlign.checked = (align == "center center" || align == "auto auto" || align == " ");
  94.   gDialog.rightAlign.checked  = (align == "right right"   || align == "auto 0px");
  95.  
  96.   if (gDialog.centerAlign.checked) {
  97.     gDialog.alignGroup.selectedItem = gDialog.centerAlign;
  98.   }
  99.   else if (gDialog.rightAlign.checked) {
  100.     gDialog.alignGroup.selectedItem = gDialog.rightAlign;
  101.   }
  102.   else {
  103.     gDialog.alignGroup.selectedItem = gDialog.leftAlign;
  104.   }
  105.  
  106.   gDialog.shading.checked = globalElement.hasAttribute("noshade");
  107. }
  108.  
  109. function onSaveDefault()
  110. {
  111.   // "false" means set attributes on the globalElement,
  112.   //   not the real element being edited
  113.   if (ValidateData()) {
  114.     var prefs = GetPrefs();
  115.     if (prefs) {
  116.  
  117.       var alignInt;
  118.       if (align == "left") {
  119.         alignInt = 0;
  120.       } else if (align == "right") {
  121.         alignInt = 2;
  122.       } else {
  123.         alignInt = 1;
  124.       }
  125.       prefs.setIntPref("editor.hrule.align", alignInt);
  126.  
  127.       var percentIndex = width.search(/%/);
  128.       var percent;
  129.       var widthInt;
  130.       var heightInt;
  131.  
  132.       if (width)
  133.       {
  134.         if (percentIndex > 0) {
  135.           percent = true;
  136.           widthInt = Number(width.substr(0, percentIndex));
  137.         } else {
  138.           percent = false;
  139.           widthInt = Number(width);
  140.         }
  141.       }
  142.       else
  143.       {
  144.         percent = true;
  145.         widthInt = Number(100);
  146.       }
  147.  
  148.       heightInt = height ? Number(height) : 2;
  149.  
  150.       prefs.setIntPref("editor.hrule.width", widthInt);
  151.       prefs.setBoolPref("editor.hrule.width_percent", percent);
  152.       prefs.setIntPref("editor.hrule.height", heightInt);
  153.       prefs.setBoolPref("editor.hrule.shading", shading);
  154.  
  155.       // Write the prefs out NOW!
  156.       var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  157.                                   .getService(Components.interfaces.nsIPrefService);
  158.       prefService.savePrefFile(null);
  159.     }
  160.     }
  161. }
  162.  
  163. // Get and validate data from widgets.
  164. // Set attributes on globalElement so they can be accessed by AdvancedEdit()
  165. function ValidateData()
  166. {
  167.   // Height is always pixels
  168.   height = ValidateNumber(gDialog.heightInput, null, 1, gMaxHRSize,
  169.                           globalElement, "size", false);
  170.   if (gValidationError)
  171.     return false;
  172.  
  173.   width = ValidateNumber(gDialog.widthInput, gDialog.pixelOrPercentMenulist, 1, maxPixels, 
  174.                          globalElement, "width", false);
  175.   if (gValidationError)
  176.     return false;
  177.  
  178.   align = "left";
  179.   if (gDialog.centerAlign.selected) {
  180.     // Don't write out default attribute
  181.     align = "";
  182.   } else if (gDialog.rightAlign.selected) {
  183.     align = "right";
  184.   }
  185.   if (align)
  186.     globalElement.setAttribute("align", align);
  187.   else
  188.     globalElement.removeAttribute("align");
  189.  
  190.   if (gDialog.shading.checked) {
  191.     shading = true;
  192.     globalElement.removeAttribute("noshade");
  193.   } else {
  194.     shading = false;
  195.     globalElement.setAttribute("noshade", "noshade");
  196.   }
  197.   return true;
  198. }
  199.  
  200. function onAccept()
  201. {
  202.   if (ValidateData())
  203.   {
  204.     // Copy attributes from the globalElement to the document element
  205.     editorShell.CloneAttributes(hLineElement, globalElement);
  206.     return true;
  207.   }
  208.   return false;
  209. }
  210.